<?php

namespace App\Http\Controllers\H5;

use App\Http\Controllers\Controller;
use App\Models\AdminUser;
use App\Models\Capital;
use App\Utils\JwtTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class AuthController extends Controller
{
    public function login(Request $request){
        $phone = $request->input("phone","");
        $password = $request->input("password","");
        $user = Capital::query()
            ->where("phone",$phone)
            ->first();
        if(empty($user)) return $this->httpError("用户不存在");
        $passworDecrypt = Crypt::decrypt($user->password);
        if($password != $passworDecrypt) return $this->httpError("密码错误");
        $tokenData = $user->toArray();
        unset($tokenData["remember_token"]);
        unset($tokenData["password"]);
        $tokenData = JwtTool::tokenData($tokenData,86400*365);
        $token = JwtTool::getToken($tokenData);
        $user->remember_token = $token;
        $user->save();
        return $this->httpSuccess([
            "token"=>$user->remember_token,
            "userInfo"=>$user,
        ]);
    }
}
